/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-12-23
 * Time: 8:14
 */
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

public class TestFoodManagement {
    private Foods foods;

    public TestFoodManagement() {
        foods = new Foods();
    }

    public void showFoodsMenu() {
        boolean con= true;
        do{
            System.out.println("满汉全席菜单系统->菜品信息管理");
            System.out.println("*******************************");
            System.out.println("\t\t1.显示所有菜品信息");
            System.out.println("\t\t2.添加菜品信息");
            System.out.println("\t\t3.修改菜品信息");
            System.out.println("\t\t4.查询菜品信息");
            System.out.println("\t\t5.返回上一层");
            System.out.println("*******************************");
            System.out.print("请输入数字选择以上菜单：");
            Scanner input=new Scanner(System.in);
            int num=input.nextInt();
            switch (num){
                case 1:
                    show();
                    con = returnLastMenu();
                    break;
                case 2:
                    add();
                    con = returnLastMenu();
                    break;
                case 3:
                    modify();
                    con = returnLastMenu();
                    break;
                case 4:
                    search();
                    con = returnLastMenu();
                    break;
                case 5:
                    con = true;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
            }

        }while (!con);
    }

    public boolean returnLastMenu() {
        System.out.println("请按'n'返回上一级菜单：");
        Scanner input = new Scanner(System.in);
        boolean con = true;
        do{
            if(input.next().equals("n")) {
                con = true;
            } else {
                System.out.println("输入错误，请重新按'n'返回上一级菜单");
                con=false;
            }
        }while (!con);
        return false;
    }

    public void add() {
        System.out.println("满汉全席菜单系统->菜品信息管理->添加菜品信息");
        String con = "n";
        do {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入菜品名称：");
            String foodsName = input.next();
            System.out.println("请输入菜品价格：");
            int foodsPrice = input.nextInt();
            foods.setFoodsName(foodsName);
            foods.setFoodsPrice(foodsPrice);

            try {
                File file = new File("foodsInfo.txt");
                if(!file.exists()){
                    file.createNewFile();
                }
                FileWriter fileWriter = new FileWriter(file.getName(), true);
                fileWriter.write(foods.getFoodsName() + "\t\t" + foods.getFoodsPrice() + "\n");
                fileWriter.close();
            } catch(IOException e){
                e.printStackTrace();
            }

            System.out.println("新菜品添加成功！");
            System.out.println("是否继续添加菜品(y/n)");
            con = input.next();
        } while ("y".equals(con) || "Y".equals(con));
    }

    public void modify() {
        System.out.println("满汉全席菜单系统->菜品信息管理->修改菜品信息");
        int index = 0;
        System.out.println("请输入要修改菜品的名称");
        Scanner input = new Scanner(System.in);
        String foodsName = input.next();

        boolean isAvailable = false;
        try (BufferedReader br = new BufferedReader(new FileReader("foodsInfo.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\t\t");
                if (parts.length > 0 && parts[0].equals(foodsName)) {
                    isAvailable = true;
                    foods.setFoodsName(parts[0]);
                    foods.setFoodsPrice(Integer.parseInt(parts[1]));
                    System.out.println("菜品名称      \t\t价格");
                    line = line.format("%-10s\t\t%.2f", foods.getFoodsName(), foods.getFoodsPrice());
                    System.out.println(line);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (isAvailable) {
            while (true) {
                System.out.println("************************************");
                System.out.println("\t\t1.修改菜品名称");
                System.out.println("\t\t2.修改菜品价格");
                System.out.println("************************************");
                System.out.println("请输入选择：");
                String oldInfo = foods.toString();
                switch (input.nextInt()) {
                    case 1:
                        System.out.println("请输入修改后的名称：");
                        foods.setFoodsName(input.next());
                        try {
                            Path path = Paths.get("foodsInfo.txt");
                            String content = new String(Files.readAllBytes(path));
                            String modifiedContent = content.replace(oldInfo, foods.toString());
                            Files.write(path, modifiedContent.getBytes(), StandardOpenOption.WRITE);
                            System.out.println("菜品名称已修改！");
                        } catch (IOException e) {
                            System.out.println("修改文件时出错" + e.getMessage());
                        }
                        break;
                    case 2:
                        System.out.println("请输入修改后的价格：");
                        foods.setFoodsPrice(input.nextInt());
                        try {
                            Path path = Paths.get("foodsInfo.txt");
                            String content = new String(Files.readAllBytes(path));
                            String modifiedContent = content.replace(oldInfo, foods.toString());
                            Files.write(path, modifiedContent.getBytes(), StandardOpenOption.WRITE);
                            System.out.println("菜品价格已修改！");
                        } catch (IOException e) {
                            System.out.println("修改文件时出错" + e.getMessage());
                        }
                        break;
                }
                System.out.println("是否修改其他属性(y/n):");
                String flag = input.next();
                if ("n".equalsIgnoreCase(flag)) {
                    break;
                }
            }
        } else {
            System.out.println("抱歉，没有你查询的菜品");
        }
    }

    public void search() {
        System.out.println("满汉全席菜单系统->菜品信息管理->查询菜品信息");
        String con = "y";
        Scanner input = new Scanner(System.in);
        while (con.equals("y")) {
            System.out.println("请输入菜品名称：");
            String foodsName = input.next();
            System.out.println("菜品名称      \t\t价格");
            boolean isAvailable = false;

            try (BufferedReader br = new BufferedReader(new FileReader("foodsInfo.txt"))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] parts = line.split("\t\t");
                    if (parts.length > 0 && parts[0].equals(foodsName)) {
                        isAvailable = true;
                        foods.setFoodsName(parts[0]);
                        foods.setFoodsPrice(Integer.parseInt(parts[1]));
                        line = line.format("%-10s\t\t%.2f", foods.getFoodsName(), foods.getFoodsPrice());
                        System.out.println(line);
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (!isAvailable) {
                System.out.println("抱歉，没有你查询的菜品");
            }
            System.out.println("是否要继续查询：(y/n)");
            con = input.next();
        }
    }

    public void show() {
        System.out.println("满汉全席菜单系统->菜品信息管理->显示菜品信息");
        System.out.println("菜品名称      \t\t价格");
        System.out.println("-------------------------------");

        try (BufferedReader br = new BufferedReader(new FileReader("foodsInfo.txt"))) {//读取文本中的所有菜品
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\t\t");
                foods.setFoodsName(parts[0]);
                foods.setFoodsPrice(Integer.parseInt(parts[1]));
                line = line.format("%-10s\t\t%.2f", foods.getFoodsName(), foods.getFoodsPrice());
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("-------------------------------");
    }

    public static void main(String[] args) {
        TestFoodManagement testFoodManagement = new TestFoodManagement();
        testFoodManagement.showFoodsMenu();
    }
}

class Foods{
    private String foodsName;
    private int foodsPrice;

    public String getFoodsName() {
        return foodsName;
    }

    public void setFoodsName(String foodsName) {
        this.foodsName = foodsName;
    }

    public double getFoodsPrice() {
        return foodsPrice;
    }

    public void setFoodsPrice(int foodsPrice) {
        this.foodsPrice = foodsPrice;
    }

    public String toString() {
        return getFoodsName() + "\t\t" + getFoodsPrice();
    }
}